<template>
  <div class="login-container">
    <div class="login-box">
      <div class="login-header">
        <h2>Vue3 Enterprise Admin</h2>
      </div>
      <div class="login-form">
        <el-form ref="loginFormRef" :model="loginForm" :rules="loginRules" label-position="top">
          <el-form-item label="用户名" prop="username">
            <el-input v-model="loginForm.username" placeholder="请输入用户名" prefix-icon="User" />
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input
              v-model="loginForm.password"
              type="password"
              placeholder="请输入密码"
              prefix-icon="Lock"
              show-password
              @keyup.enter="handleLogin"
            />
          </el-form-item>
          <el-form-item>
            <div class="login-options">
              <el-checkbox v-model="rememberMe">记住我</el-checkbox>
              <el-link type="primary">忘记密码？</el-link>
            </div>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" :loading="loading" class="login-button" @click="handleLogin">
              登录
            </el-button>
          </el-form-item>
        </el-form>
      </div>
      <div class="login-footer">
        <p>账号：admin 密码：123456</p>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue';
import { useRouter } from 'vue-router';
import { ElMessage } from 'element-plus';
import type { FormInstance, FormRules } from 'element-plus';

import { useUserStore } from '@/store/modules/user';

const router = useRouter();
const userStore = useUserStore();

// 登录表单引用
const loginFormRef = ref<FormInstance>();

// 登录表单数据
const loginForm = reactive({
  username: 'admin',
  password: '123456'
});

// 记住我
const rememberMe = ref(false);

// 加载状态
const loading = ref(false);

// 表单验证规则
const loginRules = reactive<FormRules>({
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
  ]
});

// 处理登录
const handleLogin = async () => {
  if (!loginFormRef.value) return;

  await loginFormRef.value.validate(async (valid, fields) => {
    if (valid) {
      loading.value = true;

      try {
        // 调用登录接口
        await userStore.login(loginForm.username, loginForm.password);
        // 登录成功
        ElMessage({
          message: '登录成功',
          type: 'success'
        });

        // 跳转到首页
        router.push('/');
      } catch (error: any) {
        ElMessage.error(error.message || '登录失败');
      } finally {
        loading.value = false;
      }
    } else {
      console.log('表单验证失败', fields);
    }
  });
};
</script>

<style lang="scss" scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  width: 100vw;
  background-color: var(--theme-bg-color);
  background-image: linear-gradient(
    135deg,
    rgba(57, 120, 233, 0.1) 0%,
    rgba(57, 120, 233, 0.05) 100%
  );

  .login-box {
    width: 400px;
    padding: 30px;
    background-color: var(--theme-module-bg);
    border-radius: 8px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);

    .login-header {
      text-align: center;
      margin-bottom: 30px;

      h2 {
        font-size: 24px;
        color: var(--theme-color-primary);
        margin: 0;
      }
    }

    .login-form {
      margin-bottom: 20px;

      .login-options {
        display: flex;
        justify-content: space-between;
        align-items: center;
      }

      .login-button {
        width: 100%;
        height: 40px;
        font-size: 16px;
      }
    }

    .login-footer {
      text-align: center;
      color: var(--theme-text-color);
      opacity: 0.6;
      font-size: 12px;
    }
  }
}
</style>
